package com.controller;

import com.mapper.OssFileMapper;
import com.mapper.StudentMapper;
import com.mapper.TopicMapper;
import com.mapper.UserMapper;
import com.pojo.*;
import com.utils.OssUtils;
import com.utils.Tool;
import org.apache.catalina.AccessLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 课程
@RestController
@CrossOrigin
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    TopicMapper tm;
    @Autowired
    UserMapper userMapper;
    @Autowired
    OssFileMapper ossFileMapper;
    @Autowired
    private StudentMapper sm;

    /**
     * 分页分类获取课题列表
     *
     * @param type_id  分类 id
     * @param year     年份
     * @param page     页码
     * @param pagesize 单页数据量
     */
    @RequestMapping("/getList")
    public Map<String, Object> getList(Integer type_id, String year, Integer page, Integer pagesize) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";
        if (type_id == null) {
            code = 500;
            msg = "type_id 不能为空";
        } else if (year == null) {
            code = 501;
            msg = "year 不能为空";
        } else {
            if (page == null) {
                page = 0;
            }
            if (pagesize == null) {
                pagesize = 10;
            }
            page *= pagesize;
            List<Topic> list = tm.getByTypePage(type_id, year, page, pagesize);
            for (Topic topic : list) {
                Teacher teacher = userMapper.getTeacherByOpenId(topic.getTeacher_id());
                teacher.setPassword(null);
                teacher.setLoginTime(null);
                topic.setTeacher(teacher);
            }
            map.put("total", tm.countTopic(type_id, year));
            map.put("data", list);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 获取课题详情
     *
     * @param id 课题 id
     */
    @RequestMapping("/getDetails")
    public Map<String, Object> getDetails(Integer id) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else {
            Topic topic = tm.getById(id);
            if (topic == null) {
                code = 501;
                msg = "课题不存在";
            } else {
                Teacher teacher = userMapper.getTeacherByOpenId(topic.getTeacher_id());
                teacher.setPassword(null);
                teacher.setLoginTime(null);
                topic.setTeacher(teacher);
                map.put("data", topic);
            }
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 管理员分页获取待审核课题
     *
     * @param page     页码
     * @param pagesize 单页数据量
     */
    @RequestMapping("/getInspect")
    public Map<String, Object> getInspect(Integer page, Integer pagesize) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (page == null) {
            page = 0;
        }
        if (pagesize == null) {
            pagesize = 100;
        }
        page *= pagesize;
        List<Topic> list = tm.getInspect(page, pagesize);
        for (Topic topic : list) {
            Teacher teacher = userMapper.getTeacherByOpenId(topic.getTeacher_id());
            teacher.setPassword(null);
            teacher.setLoginTime(null);
            topic.setTeacher(teacher);
        }

        map.put("code", code);
        map.put("msg", msg);
        map.put("data", list);
        map.put("total", list.size());
        return map;
    }

    /**
     * 获取教师发布的课题
     *
     * @param open_id 教师 id
     * @param type_id 类型 0-获取所有课题 1-获取审核通过的课题
     */
    @RequestMapping("/getTeacherTopic")
    public Map<String, Object> getTeacherTopic(String open_id, Integer type_id, Integer page, Integer pagesize) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";
        if (open_id == null) {
            code = 500;
            msg = "open_id 不能为空";
        } else if (type_id == null) {
            code = 501;
            msg = "type 不能为空";
        } else if (type_id > 1) {
            code = 502;
            msg = "type 参数无效";
        } else {
            if (page == null) {
                page = 0;
            }
            if (pagesize == null) {
                pagesize = 10;
            }
            page *= pagesize;
            List<Topic> list = null;
            Integer total = 0;
            if (type_id == 0) {
                list = tm.getTeacherAllTopic(open_id, page, pagesize);
                total = list.size();
            } else {
                list = tm.getTeacherApprovedTopic(open_id, page, pagesize);
                total = list.size();
            }
            for (Topic item : list) {
                Teacher teacher = userMapper.getTeacherByOpenId(item.getTeacher_id());
                teacher.setPassword(null);
                teacher.setLoginTime(null);
                item.setTeacher(teacher);
                item.setType_name(tm.getTypeNameById(item.getType_id()));
            }
            map.put("data", list);
            map.put("total", total);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    // 获取所有课题分类
    @RequestMapping("/getTypes")
    public Map<String, Object> getTypes() {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";
        map.put("code", code);
        map.put("msg", msg);
        map.put("data", tm.getTypes());
        return map;
    }

    /**
     * 添加课题分类
     *
     * @param name 分类名
     */
    @RequestMapping("/addType")
    public Map<String, Object> addType(String name) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (name == null || name == "") {
            code = 500;
            msg = "name 不能为空";
        } else {
            tm.addType(name);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 修改课题分类
     *
     * @param id   分类 id
     * @param name 分类名
     */
    @RequestMapping("/updateType")
    public Map<String, Object> updateType(Integer id, String name) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (name == null || name == "") {
            code = 501;
            msg = "name 不能为空";
        } else if (tm.getTypeById(id) == null) {
            code = 502;
            msg = "课题分类不存在";
        } else {
            tm.updateType(id, name);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 删除课题分类
     *
     * @param id 分类 id
     */
    @RequestMapping("/delType")
    public Map<String, Object> delType(Integer id) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (tm.getTypeById(id) == null) {
            code = 501;
            msg = "课题分类不存在";
        } else {
            tm.delType(id);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 管理员审核课题
     *
     * @param topic_id 课题 id
     * @param state    审核结果 1-拒绝 2-通过
     */
    @RequestMapping("/examine")
    public Map<String, Object> examine(Integer topic_id, Integer state) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (topic_id == null) {
            code = 500;
            msg = "topic_id 不能为空";
        } else if (state == null) {
            code = 501;
            msg = "state 不能为空";
        } else if (state < 1) {
            code = 502;
            msg = "state 不合法";
        } else {
            Topic topic = tm.getById(topic_id);
            if (topic.getState() != 0) {
                code = 503;
                msg = "该课题已审核";
            } else {
                tm.examine(topic_id, state);
            }
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 添加课题
     *
     * @param type_id    分类 id
     * @param open_id    教师 id
     * @param title      标题
     * @param contents    内容
     * @param img        封面
     */
    @RequestMapping("/addTopic")
    public Map<String, Object> addTopic(HttpServletRequest request,Integer type_id, String open_id, String title, String contents, @RequestParam("file") MultipartFile  img) throws IOException {
        Map<String, Object> map = new HashMap<>();
        Teacher teacher=null;
        int code = 200;
        String msg = "ok";
        String url="";
        if (type_id == null|| type_id<=0) {
            code = 500;
            msg = "type_id 不能为空";
        } else if (open_id == null|| open_id == "") {
            code = 501;
            msg = "teacher_id 不能为空";
        } else if (title == null|| title=="") {
            code = 503;
            msg = "title 不能为空";
        } else if (contents == null||contents=="") {
            code = 504;
            msg = "conents 不能为空";
        } else {
            if (img == null) {
                url="";
            }else{
                OssFile ossFile= OssUtils.uploadFile(img);
                ossFile.setOpen_id(open_id);
                teacher=userMapper.getTeacherByOpenId(open_id);
                ossFile.setUser_name(teacher.getName());
                url=ossFile.getOss_file_url();
               ossFileMapper.upLoadFile(ossFile);
            }

            assert teacher != null;
            tm.addTopic(type_id, open_id ,title, contents, url);
        }
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 修改课题
     *
     * @param id         课题 id
     * @param type_id    分类 id
     * @param open_id 教师 id
     * @param title      标题
     * @param contents    内容
     * @param img        封面
     */
    @RequestMapping("/updateTopic")
    public Map<String, Object> updateTopic(Integer id, Integer type_id, String open_id, String title, String contents, @RequestParam("file") MultipartFile  img) throws IOException {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "更新课题成功!";
        Teacher teacher=null;
        String url="";
        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (type_id == null) {
            code = 501;
            msg = "type_id 不能为空";
        } else if (open_id == null) {
            code = 502;
            msg = "open_id 不能为空";
        } else if (title == null) {
            code = 504;
            msg = "title 不能为空";
        } else if (contents == null) {
            code = 505;
            msg = "contents 不能为空";
        } else {
            OssFile ossFile= OssUtils.uploadFile(img);
            ossFile.setOpen_id(open_id);
            teacher=userMapper.getTeacherByOpenId(open_id);
            ossFile.setUser_name(teacher.getName());
            url=ossFile.getOss_file_url();
            if(ossFile.getId()==null) ossFileMapper.upLoadFile(ossFile);
            tm.updateTopic(id, type_id, teacher.getId(),title, contents, url);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 删除课题
     *
     * @param id 课题 id
     */
    @RequestMapping("/delTopic")
    public Map<String, Object> delTopic(Integer id) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (tm.getById(id) == null) {
            code = 501;
            msg = "课题不存在";
        } else {
            tm.delTopic(id);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 课题预约
     *
     * @param open_id  学生id
     * @param topic_id 课题 id
     */
    @RequestMapping("/subscribe")
    public Map<String, Object> subscribe(String open_id, Integer topic_id) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (open_id == null) {
            code = 500;
            msg = "open_id 不能为空";
        } else if (topic_id == null) {
            code = 501;
            msg = "topic_id 不能为空";
        } else if (tm.getById(topic_id).getState() !=2) {
            code = 502;
            msg = "该课题不可预约";
        }else {
            List<Topic> list=tm.getByStu_id(open_id);
            if(list.size()!=0){
                msg="该学生已有预约";
                code=502;
            }else{
                tm.subscribe(open_id, topic_id);
            }

        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 搜索课题
     *
     * @param title 课题标题
     */
    @RequestMapping("/search")
    public Map<String, Object> search(String title) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (title == null) {
            code = 500;
            msg = "title 不能为空";
        } else {
            List<Topic> list = Tool.isNull(title) ? new ArrayList<>() : tm.search("%" + title + "%");
            for (Topic topic : list) {
                Teacher teacher = userMapper.getTeacherByOpenId(topic.getTeacher_id());
                teacher.setPassword(null);
                teacher.setLoginTime(null);
                topic.setTeacher(teacher);
            }
            map.put("data", list);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    // 获取课题年份
    @RequestMapping("/getYear")
    public Map<String, Object> getYear() {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        map.put("code", code);
        map.put("msg", msg);
        map.put("data", tm.getYears());
        return map;
    }

}
